package com.kordar.rbac;

import org.apache.ibatis.annotations.MapKey;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Mapper
@Repository
public interface AuthManagerMapper {
    boolean addItem(Item item);

    Item getItem(String name);

    Set<Item> getItems(Integer type);

    Set<Item> findAllItems();

    Set<Item> getItemList(@Param("type") Integer type, @Param("names") Set<String> names);

    boolean addRule(Rule rule);

    Rule getRule(String name);

    @MapKey("name")
    Map<String, Rule> getRules();

    boolean removeItem(Item item);

    boolean removeRule(Rule rule);

    boolean updateItem(@Param("name") String name, @Param("item") Item item);

    boolean updateRule(@Param("name") String name, @Param("rule") Rule rule);

    //
    Set<Role> findRolesByUser(@Param("userId") long userId);

    boolean addItemChild(@Param("parent") String parent, @Param("child") String child);

    boolean removeChild(@Param("parent") String parent, @Param("child") String child);

    boolean hasChild(@Param("parent") String parent, @Param("child") String child);

    boolean removeChildren(@Param("parent") String parent);

    List<ItemChildren> findChildrenList();

    List<ItemChildren> findChildrenFormChild(@Param("child") String child);

    Set<Permission> findPermissionsByUser(@Param("userId") long userId);

    Set<Assignment> findAssignmentByUser(@Param("userId") long userId);

    Set<Item> findChildren(@Param("name") String name);

    // assignment
    boolean assign(@Param("itemName") String itemName, @Param("userId") Long userId, @Param("createTime") Date createTime);

    boolean removeAssignment(@Param("itemName") String itemName, @Param("userId") Long userId);

    boolean removeAllAssignmentByUser(@Param("userId") Long userId);

    boolean removeAllAssignments();

    Assignment getAssignment(@Param("itemName") String itemName, @Param("userId") Long userId);

    Set<Assignment> getAllAssignment();

    @MapKey("itemName")
    Map<String, Assignment> getAssignments(@Param("userId") Long userId);

    // delete
    void removeAll();

    void removeChildByNames(@Param("key") String key, @Param("names") Set<String> names);

    void removeAssignmentByName(@Param("names") Set<String> names);

    void removeItemByType(@Param("type") Integer type);

    void removeAllRules();
}
